package com.cmccgz.udacs_landlord_webapp.main.peoplemanager;

import android.content.Intent;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.vlayout.layout.LinearLayoutHelper;
import com.cmccgz.baselibrary.base.BaseActivity;
import com.cmccgz.baselibrary.utils.ToastUtils;
import com.cmccgz.baselibrary.view.ClearEditText;
import com.cmccgz.baselibrary.view.MyDividerItemDecoration;
import com.cmccgz.udacs_landlord_webapp.R;
import com.cmccgz.udacs_landlord_webapp.adapter.PeopleAdapter;
import com.cmccgz.udacs_landlord_webapp.base.AppConfig;
import com.cmccgz.udacs_landlord_webapp.bean.RenterBean;
import com.cmccgz.udacs_landlord_webapp.main.peoplemanager.contract.PeopleListContract;
import com.cmccgz.udacs_landlord_webapp.main.peoplemanager.presenter.PeopleListPresenter;
import com.cmccgz.udacs_landlord_webapp.utils.ViewHelper;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.List;

/**
 * @author Denis
 * @ClassName: PeopleManagerActivity
 * @Description: 租客列表页
 * @date 2017/6/7 10:48
 */

public class PeopleListActivity extends BaseActivity implements PeopleListContract.View, View.OnClickListener, XRecyclerView.LoadingListener {
    private XRecyclerView rv_func;
    private PeopleListContract.Presenter presenter;
    private PeopleAdapter peopleAdapter;
    private ViewHelper mViewHelper;
    private TextView btn_search;
    private ClearEditText et_search;
    private int currentPager = 1;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_people_list;
    }

    @Override
    protected void initView() {
        rv_func = (XRecyclerView) findViewById(R.id.rv_func);
        btn_search = (TextView) findViewById(R.id.btn_search);
        btn_search.setOnClickListener(this);
        et_search = (ClearEditText) findViewById(R.id.et_search);
        et_search.setHint("请输入租客姓名");
        rv_func.setLayoutManager(new LinearLayoutManager(mContext,LinearLayoutManager.VERTICAL,false));
        rv_func.setRefreshProgressStyle(ProgressStyle.SysProgress);
        rv_func.setLoadingMoreProgressStyle(ProgressStyle.Pacman);
        rv_func.setLoadingListener(this);
        peopleAdapter = new PeopleAdapter(this, new LinearLayoutHelper(2), null);
        peopleAdapter.setOnPeopleClickListener(new PeopleAdapter.OnPeopleClickListener() {
            @Override
            public void onPeopleClickListener(RenterBean renterBean) {
                Intent intent = new Intent(mContext, (Class<?>) getIntent().getSerializableExtra("funcClassName"));
                intent.putExtra("renterBean", renterBean);
                mContext.startActivity(intent);
            }
        });

        rv_func.setAdapter(peopleAdapter);
        rv_func.addItemDecoration(new MyDividerItemDecoration(mContext, DividerItemDecoration.VERTICAL));
        new PeopleListPresenter(this);
        presenter.initData(AppConfig.getMobile(mContext), "", "", currentPager + "");

    }

    @Override
    protected void initFragment() {

    }

    @Override
    protected void beforePermission() {
        mViewHelper = ViewHelper.instance(mContext);
        if (mViewHelper != null) {
            mViewHelper.setTitle("租客列表");
            mViewHelper.setAllVisable(false);
            mViewHelper.setBackTextVisable(true);
        }
    }

    @Override
    protected int setSystemBarColorId() {
        return 0;
    }


    @Override
    public void getData(List<RenterBean> datas) {
       /* peopleAdapter.setDatas(datas);
        peopleAdapter.notifyDataSetChanged();*/
        refreshSuccess(datas);


    }

    @Override
    public void showLoading() {
        rv_func.setRefreshing(true);
    }

    @Override
    public void refreshSuccess(List<RenterBean> data) {
        rv_func.refreshComplete();
        peopleAdapter.setDatas(data);
        peopleAdapter.notifyDataSetChanged();
    }

    @Override
    public void refreshFail(String errorMsg) {
        rv_func.refreshComplete();
        rv_func.loadMoreComplete();
        ToastUtils.show(mContext, errorMsg);
    }

    @Override
    public void loadmoreSuccess(List<RenterBean> data) {
        rv_func.refreshComplete();
        rv_func.loadMoreComplete();
        peopleAdapter.addData(data);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_back:
                PeopleListActivity.this.finish();
                break;
            case R.id.btn_search:
                currentPager = 1;
                presenter.initData(AppConfig.getMobile(mContext), et_search.getText().toString(), "", currentPager + "");
                break;
        }
    }

    @Override
    public void setPresenter(PeopleListContract.Presenter presenter) {
        this.presenter = presenter;
    }

    @Override
    public void onRefresh() {
        currentPager = 1;
        presenter.initData(AppConfig.getMobile(mContext), "", "", currentPager + "");
    }

    @Override
    public void onLoadMore() {
        currentPager++;
        presenter.initData(AppConfig.getMobile(mContext), "", "", currentPager + "");
    }
}
